This disclosure relates to memory management and memory allocation of data structures and functions.
The run-time organization of memory for a computer program often divides a system's memory into regions to store data used by the program. For example, a portion of the memory stores the executable software or code and another portion stores the variables, arguments, data, etc. used by the executable software or code. Often, local or automatic variables are stored in a stack memory structure and global variables are stored at fixed locations in a so-called global memory, and a heap memory structure can be used to store variables and other data. In programs written in C or C++ or Objective C or other C-like procedural languages, including Java, a run-time stack holds the local variables for the currently executing function or functions; each execution of a function may be referred to as an activation. The run-time stack holds the local variables for the currently executing activation as well as the activation or function which called the currently executing function. The currently executing function F1 has its data (e.g. local variables within the scope of function F1) at the top of the stack and just below the top of the stack is the data for the function F2 which called F1, and so on. Further information about stack usage can be found at pages 230-240 in the book Mac OS X Internals—A Systems Approach by Amit Singh (Addison-Wesley, 2007, Pearson Education, Inc.); these pages are incorporated herein by reference. A function's stack frame is “lost” when the function completes/exits and returns control to its caller. Hence, the local variables in the scope of the function are not retained valid in the stack after the function returns control to the caller of the function. A programmer can decide to avoid use of the stack by defining variables as global variables or by using a call, such as malloc, to allocate space for data in the heap memory structure; in this case, the stack is avoided (but can still be used for functions which use local variables that do not need to persist outside of their respective scope).
Programmers often desire to use a function or data structure known as a closure. A closure is a function that is evaluated in an environment containing zero or more bound variables. When called, the function can access these variables. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At run-time, when the outer function executes, a closure is formed and consists of the inner function's code and references to any variables of the outer function required by the closure. Memory allocation in the prior art causes the closure and all bound local variables to be initially stored in the heap memory structure (and in this case the closure is always on the heap), although certain compilers attempt to determine if a closure will never need to be stored on the heap, in which case they are allocated, at run-time, on the stack (so in this case the closure is always on the stack). In other cases, a compiler can create run-time code which will automatically migrate a closure from an initial position in the stack to the heap in response to an escape from the closure's lexical scope. In the prior art, recovery of the heap based storage is done through a garbage collector, which is uncommon for C or C based languages.